/*
 * Copyright (c) 2024. hallele, Inc All rights reserved.
 */

package cn.hallele.ms.filecenter.application.das;

import cn.hallele.infrastructure.metadata.io.Page;
import cn.hallele.infrastructure.util.ObjectUtil;
import cn.hallele.ms.filecenter.application.bo.ApiLogBO;
import cn.hallele.ms.filecenter.domain.action.apilog.ApiLogDomainService;
import cn.hallele.ms.filecenter.domain.domain.ApiLogDomain;
import cn.hallele.ms.infrastructure.core.convertor.das.ModelDas;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * hallele_ms_filecenter
 *
 * @author anle5
 * @since 2024/9/20 23:11
 */
@Slf4j
@Component
public class ApiLogBDDas extends ModelDas<ApiLogDomainService, ApiLogBO, ApiLogDomain> {
    protected ApiLogBDDas(ApiLogDomainService option) {
        super(option);
    }

    /**
     * 分页+条件查询api日志
     *
     * @param condition 查询条件
     * @param pageNo    页码
     * @param pageSize  每页数量
     * @return 查询结果
     */
    public Page<ApiLogBO> findAll(ApiLogBO condition, int pageNo, int pageSize) {
        return this.revertPage(this.option.findAll(convert(condition), pageNo, pageSize));
    }

    /**
     * 保存记录
     *
     * @param apiLogDomain 要保存的记录
     */
    public void save(ApiLogBO apiLogDomain) {
        this.option.save(convert(apiLogDomain));
    }

    /**
     * 通过id查询api日志
     *
     * @param id id
     * @return 日志记录
     */
    public ApiLogBO findOneById(Long id) {
        return this.revert(this.option.findOneById(id));
    }

    /**
     * 将T转成E
     *
     * @param apiLogBO 被转换者
     * @return S 转换后者
     */
    @Override
    protected ApiLogDomain convert(ApiLogBO apiLogBO) {
        return ObjectUtil.map(apiLogBO, bo -> ApiLogDomain.builder()
                .id(bo.getId())
                .appName(bo.getAppName())
                .apiId(bo.getApiId())
                .tranceId(bo.getTranceId())
                .ipPlace(bo.getIpPlace())
                .ip(bo.getIp())
                .clientInfo(bo.getClientInfo())
                .initiator(bo.getInitiator())
                .request(bo.getRequest())
                .response(bo.getResponse())
                .requestStartTime(bo.getRequestStartTime())
                .requestEndTime(bo.getRequestEndTime())
                .createTime(bo.getCreateTime())
                .msgCode(bo.getMsgCode())
                .msgDesc(bo.getMsgDesc())
                .build());
    }

    /**
     * 将E回转陈T
     *
     * @param apiLogDomain 被回转换者
     * @return S 回转换后者
     */
    @Override
    protected ApiLogBO revert(ApiLogDomain apiLogDomain) {
        return ObjectUtil.map(apiLogDomain, domain -> ApiLogBO.builder()
                .id(domain.getId())
                .appName(domain.getAppName())
                .apiId(domain.getApiId())
                .tranceId(domain.getTranceId())
                .ip(domain.getIp())
                .ipPlace(domain.getIpPlace())
                .clientInfo(domain.getClientInfo())
                .initiator(domain.getInitiator())
                .request(domain.getRequest())
                .response(domain.getResponse())
                .requestStartTime(domain.getRequestStartTime())
                .requestEndTime(domain.getRequestEndTime())
                .createTime(domain.getCreateTime())
                .msgCode(domain.getMsgCode())
                .msgDesc(domain.getMsgDesc())
                .build());
    }
}
